<template>
  <div class="page">
    <headModule />
    <div class="miva-wapper">
      <bannerModule />
      <advModuleByFloor href="https://www.baidu.com" />
      <div class="zone-wrap-module clearfix" id="音频" ref="floor_1">
        <newComersModule :isFail="audioModuleFail" @load="getAudioListByNewHandler" to="/audio" title="音频">
          <spreadModule :key="audio.aid" :value="audio" class="item-spread-module" v-for="audio in audioList" />
        </newComersModule>
        <rankingTimeIngModule :limit="7" :rankingData="audioRankingData" :type="2" class="zone-rank" to="/audio" />
      </div>
      <advModuleByFloor href="https://www.baidu.com" />
      <div class="zone-wrap-module clearfix" id="文章" ref="floor_2">
        <newComersModule :isFail="articleModuleFail" @load="getArticleHandler" to="/gameArchive" title="文章">
          <articleModule :article="article" :key="article.aid" v-for="article in articleList" />
        </newComersModule>
        <rankingTimeIngModule :limit="11" :rankingData="audioRankingData" :type="4" class="zone-rank" />
      </div>
    </div>
    <elevatorModule :value="elevatorData" />
    <!-- <footerModule size="small" /> -->
    <footerModule />
  </div>
</template>

<script>
import headModule from '@/components/header/index.vue';
import elevatorModule from '@/components/elevatorModule/index.vue';
import bannerModule from '@/components/bannerModule/index.vue';
import newComersModule from '@/components/newComersModule/index.vue';
import spreadModule from '@/components/spreadModule.vue';
import rankingTimeIngModule from '@/components/rankingTimeing/index.vue';
import footerModule from '@/components/footer/index.vue';
import advModuleByFloor from '@/components/advModule/model_1.vue';
import articleModule from '@/components/articleModule/index.vue';
import { getArchivesByTypeAPI } from '@/api/archive';
import { getElementTop } from '@/utils/index';

export default {
    name: 'Home',
    components: {
        headModule,
        elevatorModule,
        bannerModule,
        advModuleByFloor,
        newComersModule,
        spreadModule,
        articleModule,
        rankingTimeIngModule,
        footerModule,
    },
    data() {
        return {
            audioModuleFail: false,
            articleModuleFail: false,
            elevatorData: [],
            audioList: [],
            articleList: [],
            audioRankingData: [],
        };
    },
    mounted() {
        this.getElevatorData();
    },
    methods: {
        getElevatorData() {
            const elements = this.$refs;
            this.elevatorData = Object.values(elements).map(element => ({ title: element.id, toScroll: getElementTop(element) }));
        },
        async  getAudioListByNewHandler() {
            try {
                const { data } = await getArchivesByTypeAPI(2, 1, 10);
                this.audioList = data;
            } catch (error) {
                this.audioModuleFail = true;
                console.error(error);
            }
        },
        async  getArticleHandler() {
            try {
                const { data } = await getArchivesByTypeAPI(4, 1, 10);
                this.articleList = data;
            } catch (error) {
                this.articleModuleFail = true;
                console.error(error);
            }
        },
    },
};
</script>

<style lang="less">
.miva-wapper {
  width: @baseWidth;
  // width: @bigWidth;
  margin-bottom: 50px;
}
.home-adv {
  width: @baseWidth;
  height: 109px;
  object-fit: cover;
}
.zone-wrap-module {
  display: flex;
  padding-top: 30px;
  margin-bottom: 50px;
  justify-content: center;
  .zone-rank {
    float: right;
    width: 260px;
    min-height: 360px;
    flex-shrink: 0;
  }
  .item-spread-module {
    float: left;
  }
}
</style>
